package com.alisls.demo.springcloud.service.im.config;

import com.alisls.demo.springcloud.service.im.handler.MyTextMessageHandler;
import com.alisls.demo.springcloud.service.im.interceptor.MyHandshakeInterceptor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * 描述信息
 *
 * @author Ke Wang
 * @date 2020/10/14
 */
@Configuration
@RefreshScope
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private MyHandshakeInterceptor myHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry
                // 添加处理器
                .addHandler(myTextMessageHandler(), "/ws/{id}")
                // 添加握手拦截器
                .addInterceptors(myHandshakeInterceptor)
                // 设置跨域
                .setAllowedOrigins("*");
    }

    @Bean
    public MyTextMessageHandler myTextMessageHandler() {
        return new MyTextMessageHandler();
    }

}
